package com.example.memento.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.Builder;

import java.time.LocalDateTime;
import java.util.List;

/**
 * API响应包装类
 * 统一的API响应格式
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@JsonIgnoreProperties(ignoreUnknown = true)
public class ApiResponse<T> {
    
    private T data;
    
    private String message;
    
    private String error;
    
    private Boolean success;
    
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime timestamp;
    
    private Integer code;
    
    /**
     * 成功响应
     */
    public static <T> ApiResponse<T> success(T data) {
        return ApiResponse.<T>builder()
                .data(data)
                .success(true)
                .code(200)
                .timestamp(LocalDateTime.now())
                .build();
    }
    
    /**
     * 成功响应带消息
     */
    public static <T> ApiResponse<T> success(T data, String message) {
        return ApiResponse.<T>builder()
                .data(data)
                .message(message)
                .success(true)
                .code(200)
                .timestamp(LocalDateTime.now())
                .build();
    }
    
    /**
     * 失败响应
     */
    public static <T> ApiResponse<T> error(String error) {
        return ApiResponse.<T>builder()
                .error(error)
                .success(false)
                .code(500)
                .timestamp(LocalDateTime.now())
                .build();
    }
    
    /**
     * 失败响应带状态码
     */
    public static <T> ApiResponse<T> error(String error, Integer code) {
        return ApiResponse.<T>builder()
                .error(error)
                .success(false)
                .code(code)
                .timestamp(LocalDateTime.now())
                .build();
    }
    
    /**
     * 失败响应带消息和错误
     */
    public static <T> ApiResponse<T> error(String message, String error, Integer code) {
        return ApiResponse.<T>builder()
                .message(message)
                .error(error)
                .success(false)
                .code(code)
                .timestamp(LocalDateTime.now())
                .build();
    }
    
    /**
     * 检查是否成功
     */
    public boolean isSuccess() {
        return Boolean.TRUE.equals(success);
    }
    
    /**
     * 检查是否有错误
     */
    public boolean hasError() {
        return error != null && !error.isEmpty();
    }
    
    /**
     * 检查是否有数据
     */
    public boolean hasData() {
        return data != null;
    }
}
